package com.haiyou.common.util.bean;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
import java.util.Map;


import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Lists;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DomainEquals {

	
	
	/**
	 * 对比2个bean 的数据 单层 无法处理套娃
	 * @param source
	 * @param target
	 * @return
	 */
    public static boolean domainEquals(Object source, Object target) {
        
    	if (source == null || target == null) {
            return false;
        }
        
        
        Method[] methods =  source.getClass().getMethods();
        
        for (Method method : methods) {
        	
        	 if (!method.getName().startsWith("get")) {
                 continue;
             }
        	
        	if( method.getParameterTypes().length != 0) {
        		continue;
        	}
        	 
        	try {
				
        		Object v1 = null;
				Object v2= null;
				try {
					v1 = method.invoke(source);
					v2 = method.invoke(target);
				} catch (Exception e) {
					log.info("source:{},method:{} 错误",source.getClass().getSimpleName(),method.getName());
					continue;
				}
				
				if(v1 instanceof Map) {
					
					Map map1 = (Map)v1;
					Map map2 = (Map)v2;

					for (Object key : map1.keySet()) {
						
						Object o1 = map1.get(key);
						Object o2 = map2.get(key);

						if(o1 == null) {
							return false;
						}
						
						if(o2 == null) {
							return false;
						}
						
						if(!JSONObject.toJSONString(o1).equals(JSONObject.toJSONString(o2))) {
							log.info("source:{},method:{} 错误",source.getClass().getSimpleName(),method.getName());
							log.info(JSONObject.toJSONString(o1));
							log.info(JSONObject.toJSONString(o2));
							return false;
						}
					}
					
				}else if(v1 instanceof Iterable) {
					
					Iterable iterable1 = (Iterable)v1;
					Iterable iterable2 = (Iterable)v2;

					List<String> list1 = Lists.newArrayList();
					List<String> list2 = Lists.newArrayList();

					iterable1.forEach(x->{
						list1.add(JSONObject.toJSONString(x));
					});
					
					iterable2.forEach(x->{
						list2.add(JSONObject.toJSONString(x));
					});
					
					//排序
					
					Collections.sort(list1);
					Collections.sort(list2);
					
					if(!JSONObject.toJSONString(list1).equals(JSONObject.toJSONString(list2))) {
						log.info("source:{},method:{} 错误",source.getClass().getSimpleName(),method.getName());
						log.info(JSONObject.toJSONString(list1));
						log.info(JSONObject.toJSONString(list2));
						return false;
					}
					
				}else {
					//默认为基础类型 转json 处理
					if(!JSONObject.toJSONString(v1).equals(JSONObject.toJSONString(v2))) {
						log.info("source:{},method:{} 错误",source.getClass().getSimpleName(),method.getName());
						log.info(JSONObject.toJSONString(v1));
						log.info(JSONObject.toJSONString(v2));
						return false;
					}
				}
			} catch (Exception e) {
				log.error(e.getMessage(),e);
			} 
        	
		}
        
        return true;
    }
 

 
}
